package invalidservicedependency;

import java.util.Arrays;
import org.eclipse.viatra2.emf.incquery.runtime.api.IPatternMatch;
import org.eclipse.viatra2.emf.incquery.runtime.api.impl.BasePatternMatch;
import org.eclipse.viatra2.patternlanguage.core.patternLanguage.Pattern;
import sysmodel.BusinessService;
import sysmodel.RestService;

/**
 * Pattern-specific match representation of the InvalidServiceDependency pattern, 
 * to be used in conjunction with InvalidServiceDependencyMatcher.
 * 
 * <p>Class fields correspond to parameters of the pattern. Fields with value null are considered unassigned.
 * Each instance is a (possibly partial) substitution of pattern parameters, 
 * usable to represent a match of the pattern in the result of a query, 
 * or to specify the bound (fixed) input parameters when issuing a query.
 * 
 * @see InvalidServiceDependencyMatcher
 * @see InvalidServiceDependencyProcessor
 * 
 */
public final class InvalidServiceDependencyMatch extends BasePatternMatch implements IPatternMatch {
  private BusinessService fService1;
  
  private RestService fService2;
  
  private static String[] parameterNames = {"service1", "service2"};
  
  public InvalidServiceDependencyMatch(final BusinessService pService1, final RestService pService2) {
    this.fService1 = pService1;
    this.fService2 = pService2;
    
  }
  
  @Override
  public Object get(final String parameterName) {
    if ("service1".equals(parameterName)) return this.fService1;
    if ("service2".equals(parameterName)) return this.fService2;
    return null;
    
  }
  
  public BusinessService getService1() {
    return this.fService1;
    
  }
  
  public RestService getService2() {
    return this.fService2;
    
  }
  
  @Override
  public boolean set(final String parameterName, final Object newValue) {
    if ("service1".equals(parameterName) && newValue instanceof sysmodel.BusinessService) {
    	this.fService1 = (sysmodel.BusinessService) newValue;
    	return true;
    }
    if ("service2".equals(parameterName) && newValue instanceof sysmodel.RestService) {
    	this.fService2 = (sysmodel.RestService) newValue;
    	return true;
    }
    return false;
    
  }
  
  public void setService1(final BusinessService pService1) {
    this.fService1 = pService1;
    
  }
  
  public void setService2(final RestService pService2) {
    this.fService2 = pService2;
    
  }
  
  @Override
  public String patternName() {
    return "InvalidServiceDependency";
    
  }
  
  @Override
  public String[] parameterNames() {
    return InvalidServiceDependencyMatch.parameterNames;
    
  }
  
  @Override
  public Object[] toArray() {
    return new Object[]{fService1, fService2};
    
  }
  
  @Override
  public String prettyPrint() {
    StringBuilder result = new StringBuilder();
    result.append("\"service1\"=" + prettyPrintValue(fService1) + ", ");
    
    result.append("\"service2\"=" + prettyPrintValue(fService2)
    );return result.toString();
    
  }
  
  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((fService1 == null) ? 0 : fService1.hashCode()); 
    result = prime * result + ((fService2 == null) ? 0 : fService2.hashCode()); 
    return result; 
    
  }
  
  @Override
  public boolean equals(final Object obj) {
    if (this == obj)
    	return true;
    if (obj == null)
    	return false;
    if (!(obj instanceof IPatternMatch))
    	return false;
    IPatternMatch otherSig  = (IPatternMatch) obj;
    if (!pattern().equals(otherSig.pattern()))
    	return false;
    if (!InvalidServiceDependencyMatch.class.equals(obj.getClass()))
    	return Arrays.deepEquals(toArray(), otherSig.toArray());
    				InvalidServiceDependencyMatch other = (InvalidServiceDependencyMatch) obj;
    				if (fService1 == null) {if (other.fService1 != null) return false;}
    				else if (!fService1.equals(other.fService1)) return false;
    				if (fService2 == null) {if (other.fService2 != null) return false;}
    				else if (!fService2.equals(other.fService2)) return false;
    				return true;
    
  }
  
  @Override
  public Pattern pattern() {
    return InvalidServiceDependencyMatcher.FACTORY.getPattern();
  }
}
